<template>
  <view 
    class="custom-card"
    :class="{
      'shadow': shadow,
      'bordered': bordered,
      'hoverable': hoverable,
      'disabled': disabled,
      'loading': loading,
      [`size-${size}`]: size,
      [`variant-${variant}`]: variant,
      [`radius-${radius}`]: radius
    }"
    :style="customStyles"
    @click="handleClick"
  >
    <!-- 卡片头部 -->
    <view v-if="header || $slots.header" class="card-header">
      <slot name="header">
        <view class="header-content">
          <view v-if="title" class="header-title">{{ title }}</view>
          <view v-if="subtitle" class="header-subtitle">{{ subtitle }}</view>
        </view>
        <view v-if="$slots.headerAction" class="header-action">
          <slot name="headerAction"></slot>
        </view>
      </slot>
    </view>
    
    <!-- 卡片内容 -->
    <view class="card-body">
      <slot></slot>
    </view>
    
    <!-- 卡片底部 -->
    <view v-if="footer || $slots.footer" class="card-footer">
      <slot name="footer">
        {{ footer }}
      </slot>
    </view>
    
    <!-- 加载状态覆盖层 -->
    <view v-if="loading" class="loading-overlay">
      <uni-loading type="spinner" color="#2196F3" :size="loadingSize" />
      <view v-if="loadingText" class="loading-text">{{ loadingText }}</view>
    </view>
    
    <!-- 禁用状态覆盖层 -->
    <view v-if="disabled" class="disabled-overlay"></view>
    
    <!-- 图标装饰 -->
    <view v-if="icon" class="card-icon" :class="iconPosition">
      <uni-icon 
        :type="icon" 
        :size="iconSize || 24" 
        :color="iconColor || '#2196F3'" 
      />
    </view>
  </view>
</template>

<script setup>
import { computed } from 'vue';
import { themeConfig, themeUtils } from '../../theme/themeConfig';

// 定义props
const props = defineProps({
  // 基本属性
  title: {
    type: String,
    default: ''
  },
  subtitle: {
    type: String,
    default: ''
  },
  footer: {
    type: String,
    default: ''
  },
  
  // 样式属性
  size: {
    type: String,
    default: 'medium',
    validator: (value) => ['small', 'medium', 'large'].includes(value)
  },
  variant: {
    type: String,
    default: 'default',
    validator: (value) => ['default', 'primary', 'secondary', 'success', 'warning', 'error', 'info', 'transparent'].includes(value)
  },
  radius: {
    type: String,
    default: 'medium',
    validator: (value) => ['none', 'small', 'medium', 'large', 'full'].includes(value)
  },
  shadow: {
    type: Boolean,
    default: true
  },
  bordered: {
    type: Boolean,
    default: false
  },
  
  // 状态属性
  disabled: {
    type: Boolean,
    default: false
  },
  loading: {
    type: Boolean,
    default: false
  },
  loadingText: {
    type: String,
    default: ''
  },
  loadingSize: {
    type: String,
    default: '24'
  },
  hoverable: {
    type: Boolean,
    default: false
  },
  
  // 图标属性
  icon: {
    type: String,
    default: ''
  },
  iconSize: {
    type: [String, Number],
    default: 24
  },
  iconColor: {
    type: String,
    default: ''
  },
  iconPosition: {
    type: String,
    default: 'top-right',
    validator: (value) => ['top-left', 'top-right', 'bottom-left', 'bottom-right'].includes(value)
  },
  
  // 自定义样式
  backgroundColor: {
    type: String,
    default: ''
  },
  textColor: {
    type: String,
    default: ''
  },
  borderColor: {
    type: String,
    default: ''
  },
  padding: {
    type: String,
    default: ''
  },
  margin: {
    type: String,
    default: ''
  },
  width: {
    type: String,
    default: ''
  },
  height: {
    type: String,
    default: ''
  },
  minHeight: {
    type: String,
    default: ''
  },
  maxHeight: {
    type: String,
    default: ''
  },
  
  // 交互属性
  clickable: {
    type: Boolean,
    default: false
  },
  ripple: {
    type: Boolean,
    default: false
  }
});

// 定义事件
const emit = defineEmits(['click', 'hover', 'focus', 'blur']);

// 计算自定义样式
const customStyles = computed(() => {
  const styles = {};
  
  // 背景颜色
  if (props.backgroundColor) {
    styles.backgroundColor = props.backgroundColor;
  } else if (props.variant !== 'default' && props.variant !== 'transparent') {
    styles.backgroundColor = themeUtils.getColor(props.variant, 'base');
  }
  
  // 文本颜色
  if (props.textColor) {
    styles.color = props.textColor;
  } else if (props.variant !== 'default' && props.variant !== 'transparent') {
    const contrastColor = themeUtils.getColor(props.variant, 'contrast');
    if (contrastColor) {
      styles.color = contrastColor;
    }
  }
  
  // 边框颜色
  if (props.bordered && props.borderColor) {
    styles.borderColor = props.borderColor;
  }
  
  // 内边距
  if (props.padding) {
    styles.padding = props.padding;
  }
  
  // 外边距
  if (props.margin) {
    styles.margin = props.margin;
  }
  
  // 尺寸相关
  if (props.width) styles.width = props.width;
  if (props.height) styles.height = props.height;
  if (props.minHeight) styles.minHeight = props.minHeight;
  if (props.maxHeight) styles.maxHeight = props.maxHeight;
  
  return styles;
});

// 处理点击事件
function handleClick(event) {
  if (!props.disabled && !props.loading && (props.clickable || props.hoverable)) {
    emit('click', event);
  }
}
</script>

<style scoped>
/* 基础卡片样式 */
.custom-card {
  background-color: #FFFFFF;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s ease;
  position: relative;
  box-sizing: border-box;
}

/* 尺寸变体 */
.custom-card.size-small {
  padding: 12px;
}

.custom-card.size-medium {
  padding: 16px;
}

.custom-card.size-large {
  padding: 24px;
}

/* 圆角变体 */
.custom-card.radius-none {
  border-radius: 0;
}

.custom-card.radius-small {
  border-radius: 4px;
}

.custom-card.radius-medium {
  border-radius: 8px;
}

.custom-card.radius-large {
  border-radius: 12px;
}

.custom-card.radius-full {
  border-radius: 9999px;
}

/* 阴影效果 */
.custom-card.shadow {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 边框效果 */
.custom-card.bordered {
  border: 1px solid #E0E0E0;
}

/* 变体样式 */
.custom-card.variant-primary {
  background-color: #2196F3;
  color: #FFFFFF;
}

.custom-card.variant-secondary {
  background-color: #FF9800;
  color: #FFFFFF;
}

.custom-card.variant-success {
  background-color: #4CAF50;
  color: #FFFFFF;
}

.custom-card.variant-warning {
  background-color: #FFC107;
  color: #212121;
}

.custom-card.variant-error {
  background-color: #F44336;
  color: #FFFFFF;
}

.custom-card.variant-info {
  background-color: #00BCD4;
  color: #FFFFFF;
}

.custom-card.variant-transparent {
  background-color: transparent;
}

/* 交互效果 */
.custom-card.hoverable {
  cursor: pointer;
}

.custom-card.hoverable:active {
  transform: translateY(2px);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

/* 卡片头部 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid #F5F5F5;
}

.header-content {
  flex: 1;
  min-width: 0;
}

.header-title {
  font-size: 16px;
  font-weight: 600;
  color: inherit;
  margin-bottom: 4px;
}

.header-subtitle {
  font-size: 13px;
  color: #757575;
}

.custom-card[class*="variant-"]:not(.variant-transparent) .header-subtitle {
  color: rgba(255, 255, 255, 0.8);
}

.header-action {
  margin-left: 12px;
}

/* 卡片内容 */
.card-body {
  min-height: 20px;
}

/* 卡片底部 */
.card-footer {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid #F5F5F5;
  font-size: 13px;
  color: #757575;
}

.custom-card[class*="variant-"]:not(.variant-transparent) .card-footer {
  color: rgba(255, 255, 255, 0.8);
}

/* 加载状态 */
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  z-index: 10;
}

.custom-card[class*="variant-"]:not(.variant-transparent) .loading-overlay {
  background-color: rgba(0, 0, 0, 0.4);
}

.loading-text {
  font-size: 14px;
  color: #666;
  margin-top: 8px;
}

.custom-card[class*="variant-"]:not(.variant-transparent) .loading-text {
  color: #FFFFFF;
}

/* 禁用状态 */
.disabled-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.1);
  cursor: not-allowed;
  z-index: 10;
}

/* 图标装饰 */
.card-icon {
  position: absolute;
  z-index: 1;
}

.card-icon.top-left {
  top: 12px;
  left: 12px;
}

.card-icon.top-right {
  top: 12px;
  right: 12px;
}

.card-icon.bottom-left {
  bottom: 12px;
  left: 12px;
}

.card-icon.bottom-right {
  bottom: 12px;
  right: 12px;
}

/* 尺寸调整 */
.size-small .header-title {
  font-size: 14px;
}

.size-small .header-subtitle {
  font-size: 12px;
}

.size-small .card-footer {
  font-size: 12px;
}

.size-large .header-title {
  font-size: 18px;
}

.size-large .header-subtitle {
  font-size: 14px;
}

.size-large .card-footer {
  font-size: 14px;
}

/* 移除内部边距 */
.custom-card.no-padding {
  padding: 0;
}

.custom-card.no-padding .card-header,
.custom-card.no-padding .card-body,
.custom-card.no-padding .card-footer {
  padding: 16px;
}

/* 响应式调整 */
@media (max-width: 360px) {
  .card-header {
    flex-direction: column;
    gap: 8px;
  }
  
  .header-action {
    margin-left: 0;
    align-self: flex-start;
  }
}
</style>